﻿using MJTop.BLL.SYS;
using MJTop.Model.SYS;
using MJTop.Web.Core;
using MJTop.Web.Core.Filters;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;

namespace MJTop.WebApp.Controllers
{
    public class MenuController : BaseController
    {
        // GET: Menu
        public ActionResult Index()
        {
            return View();
        }

        #region 主页初始化菜单
        [AjaxOnly]
        public ActionResult InitMainMenu()
        {
            var res = MenuBLL.GetMainMenu(CurrentUser);
            if (res.Code == -1)
            {
                return this.JsonNet(res);
            }
            else
            {
                List<Tree> lstMain = res.Value;
                if (lstMain == null || lstMain.Count == 0)
                {
                    return this.JsonFail("系统中没有菜单！");
                }
                else
                {
                    StringBuilder sbHtml = new StringBuilder();
                    bool isFirst = true;
                    foreach (var menuMain in lstMain)
                    {
                        if (isFirst)
                        {
                            sbHtml.Append("<li class='active' >");
                            isFirst = false;
                        }
                        else
                        {
                            sbHtml.Append("<li>");
                        }

                        string strHref = string.Format("/Menu/InitChildMenu?pid={0}", menuMain.id);
                        if (string.IsNullOrWhiteSpace(menuMain.icon))
                        {
                            sbHtml.AppendFormat("<a href='{0}'  data-toggle='sidenav' data-id-key='targetid'>{1}</a>", strHref, menuMain.name);
                        }
                        else
                        {
                            sbHtml.AppendFormat("<a href='{0}'  data-toggle='sidenav' data-id-key='targetid'><i class=\"fa fa-" + menuMain.icon + "\"></i>&nbsp;{1}</a>", strHref, menuMain.name);
                        }
                        sbHtml.Append("</li>");
                    }                   
                    return this.JsonSuccess(sbHtml.ToString());
                }
            }
        }

        [AjaxOnly]
        public ActionResult InitChildMenu(string pid)
        {
            var res = MenuBLL.GetChildMenu(pid, CurrentUser);
            if (res.Code == -1)
            {
                return this.JsonNet(res);
            }
            else
            {
                List<Tree> lstChildTree = res.Value;
                if (lstChildTree == null || lstChildTree.Count == 0)
                {
                    return this.JsonFail("该菜单下没有子菜单！");
                }
                else
                {
                    return this.JsonNet(lstChildTree);
                }
            }
        }
        #endregion

        [HttpPost]
        public ActionResult GetMenuTree()
        {
            return this.JsonNet(MenuBLL.LST_ALL_MENU);
        }

        [HttpPost]
        public ActionResult MenuUpsert()
        {
            var ret = MenuBLL.UpsertTree(Request.Form, CurrentUser);
            if (ret.Code == -1)
            {
                return this.JsonBJUI(300, ret.Msg);
            }
            return this.JsonBJUI(200, ret.Msg);
        }

        [HttpPost]
        public ActionResult DelMenu(string Id)
        {
            var ret = MenuBLL.DelMenu((Id ?? string.Empty).Split(','));
            if (ret.Code == -1)
            {
                return this.JsonBJUI(300, ret.Msg);
            }
            return this.JsonBJUI(200, ret.Msg);
        }
    }
}